package com.qs.home.ui.VM;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.qs.base.router.RouterActivityPath;
import com.qs.home.BR;
import com.qs.home.R;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

/**
 * 识别结果VM
 *
 * @Author ltzz
 * @Date 2019/8/15
 */
public class IdentifyResultsViewModel extends BaseViewModel {
    public IdentifyResultsViewModel(@NonNull Application application) {
        super(application);
    }


    //给RecyclerView添加ObservableList
    public ObservableList<IdentifyResultsItemViewModel> observableList = new ObservableArrayList<>();
    //RecyclerView多布局添加ItemBinding
    public ItemBinding<IdentifyResultsItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<IdentifyResultsItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, IdentifyResultsItemViewModel item) {
            //设置布局
            itemBinding.set(BR.viewModel, R.layout.home_identify_results_item);
        }
    });
    //给RecyclerView添加Adpter，请使用自定义的Adapter继承BindingRecyclerViewAdapter，重写onBindBinding方法，里面有你要的Item对应的binding对象
    public BindingRecyclerViewAdapter<IdentifyResultsItemViewModel> adapter = new BindingRecyclerViewAdapter<IdentifyResultsItemViewModel>() {
        @Override
        public void onBindBinding(ViewDataBinding binding, int variableId, int layoutRes, int position, final IdentifyResultsItemViewModel item) {
            super.onBindBinding(binding, variableId, layoutRes, position, item);
            binding.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //详情点击事件
                    Gson gson = new Gson();
                    ARouter.getInstance().build(RouterActivityPath.Home.IDENTIFY_DETAILS).withString("textResult", gson.toJson(item.mItemEntity.get())).navigation();
                }
            });
        }
    };
}
